/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.panels;

import com.openwaf.client.core.Window;
import com.openwaf.client.dom.DivElement;
import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.LIElement;
import com.openwaf.client.dom.Node;
import com.openwaf.client.dom.OListElement;
import com.openwaf.client.dom.SpanElement;
import com.openwaf.client.event.dom.ClickEvent;
import com.openwaf.client.event.dom.ClickHandler;
import com.openwaf.client.ui.UIControl;
import com.openwaf.client.ui.annotation.ViewElement;
import com.openwaf.client.ui.annotation.ViewMethod;
import com.openwaf.client.ui.annotation.ViewProperty;
import java.util.ArrayList;
import java.util.Iterator;

public class TabPanel extends UIControl implements Iterable<TabPanelElement>{
    private ArrayList<TabPanelElement> tabs;
    @ViewElement
    private DivElement divTitle;
    @ViewElement
    private DivElement divContent;
    private int selectedIndex;
    public static String CSS_TITLE_NORMAL="waf-panel-tab-title";
    public static String CSS_TITLE_SELECTED="waf-panel-tab-title-selected";

    private String cssTitleNormal=CSS_TITLE_NORMAL;
    private String cssTitleSelected=CSS_TITLE_SELECTED;

    @ViewProperty
    public void setCssTitleNormal(String name){
        cssTitleNormal=name;
        updateCss();
    }
    public String getCssTitleNormal(){
        return cssTitleNormal;
    }

    @ViewProperty
    public void setCssTitleSelected(String name){
        cssTitleSelected=name;
        updateCss();
    }
    public String getCssTitleSelected(){
        return cssTitleSelected;
    }

    @ViewProperty
    public void setCssTitleGroup(String name){
        divTitle.setCssClassName(name);
    }
    public String getCssTitleGroup(){
        return divTitle.getCssClassName();
    }

    @ViewProperty
    public void setCssContent(String name){
        divContent.setCssClassName(name);
    }
    public String getCssContent(){
        return divContent.getCssClassName();
    }
    private void updateCss(){
        int i,l=divTitle.getChildCount();
        for(i=0;i<l;i++){
            Element e=(Element)(divTitle.getChildAt(i));
            if(i==selectedIndex){
                e.setCssClassName(cssTitleSelected);
            }else{
                e.setCssClassName(cssTitleNormal);
            }
        }
    }
    public TabPanel(){
       selectedIndex=-1;
       tabs=new ArrayList<TabPanelElement>();
    }
    @ViewMethod
    public void addElement(final TabPanelElement tab){        
        SpanElement span=(SpanElement)Window.getDocument().createElement(SpanElement.TAG);                
        span.setCssClassName(cssTitleNormal);
        span.setInnerHTML(tab.getTitle());
        divTitle.appendChild(span);
        span.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                select(tab);
            }
        });
        tabs.add(tab);
        if(tabs.size()==1){
            select(0);
        }
    }
    public void addElement(int index,final TabPanelElement tab){
        if(index<0 || index >= tabs.size()){
            throw new IndexOutOfBoundsException("Index:"+index+",Size:"+tabs.size());
        }
        LIElement li=(LIElement)Window.getDocument().createElement(LIElement.TAG);
        SpanElement span=(SpanElement)Window.getDocument().createElement(SpanElement.TAG);
        li.appendChild(span);
        li.getStyle().setDisplay("inline");
        span.setCssClassName(cssTitleNormal);
        span.setInnerHTML(tab.getTitle());
        Node ex=divTitle.getChildAt(index);
        divTitle.insertBefore(li, ex);
        span.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                select(tab);
            }
        });
        tabs.add(index,tab);
    }

    private void select(int index){
        if(index<0 || index >= tabs.size()){
            throw new IndexOutOfBoundsException("Index:"+index+",Size:"+tabs.size());
        }
        if(selectedIndex>=0){
            ((Element)divTitle.getChildAt(selectedIndex)).setCssClassName(cssTitleNormal);
        }
        divContent.clearChilds();
        TabPanelElement tab=tabs.get(index);
        divContent.appendChild(tab.getElement());
        ((Element)divTitle.getChildAt(index)).setCssClassName(cssTitleSelected);
        selectedIndex=index;

    }
    private void select(TabPanelElement tab){
        int index=getIndex(tab);
        if(index<0){ return; }
        select(index);        
    }
    public int getIndex(TabPanelElement tab){
        int count=0;
        for(TabPanelElement t:tabs){
            if(t.equals(tab)){
                return count;
            }
            count+=1;            
        }
        return -1;
    }
    
    public Iterator<TabPanelElement> iterator(){
        return tabs.iterator();
    }

}
